const express = require("express")
const router = express.Router()
const time = require("../../utils/time")
const db = require("../../sqlServer/dbConnect")

// 食品基本信息-- 查询
router.post("/info", (req, res) => {
    const { values, pageConfig, doName } = req.body;
    const { page, pageSize } = pageConfig
    let sql = values === null ? "select * from notice order by Id limit ?,?;select * from notice" : 
    "select * from notice where date=? limit ?,?;select * from notice where date=?"
    const data = values === null ? [(Number(page) - 1) * Number(pageSize), Number(pageSize)] : 
    [values.ftime,(Number(page) - 1) * Number(pageSize), Number(pageSize),values.fname,values.ftime]
    db.sqlQuery(sql, data, (err, result) => {
        if (err) {
            console.log(err)
            res.send({
                // data: result,
                msg: "获取信息失败",
                status: 203
            })
        } else {
                res.send({
                    data: result[0],
                    total: result[1].length,
                    msg: "获取信息成功",
                    status: 200
                })
        }
    })
    db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)", [doName, "更新通告查询", time.getData()])
})

// 食品基本信息-- 添加
router.post("/add", (req, res) => {
    const { notice,date, doName } = req.body
    const code = parseInt(Math.random() * 100000000)
    let sql = "insert into notice(notice,date) values(?,?)"
    const data = [notice,date]
    db.sqlQuery(sql, data, (err) => {
        if (err) {
            console.log(err)
            if (err.errno === 1048) {
                res.send({
                    msg: "添加失败,不能为空!",
                    status: 203
                })
            }else {
                res.send({
                    msg: "添加失败!",
                    status: 203
                })
            }
        } else {
            res.send({
                msg: "添加成功",
                status: 200
            })
            db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)", [doName, "更新通告添加", time.getData()])
        }
    })
})

// 食品基本信息-- 编辑
router.post("/edit", (req, res) => {
    const { notice,date, Id, doName } = req.body
    let sql = "update notice set notice=?,date=? where Id=?"
    const data = [notice,date, Id]
    db.sqlQuery(sql, data, (err) => {
        if (err) {
            console.log(err)
            res.send({
                msg: "编辑失败",
                status: 203
            })
        } else {
            res.send({
                msg: "编辑成功",
                status: 200
            })
            db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)", [doName, "更新通告编辑", time.getData()])
        }
    })
})

// 食品基本信息-- 删除
router.post("/delete", (req, res) => {
    const { Id, doName } = req.body
    let sql = "delete from notice where Id=?"
    const data = [Id]
    db.sqlQuery(sql, data, (err) => {
        if (err) {
            console.log(err)
            res.send({
                msg: "删除失败",
                status: 203
            })
        } else {
            res.send({
                msg: "删除成功",
                status: 200
            })
            db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)", [doName, "更新通告删除", time.getData()])
        }
    })
})

module.exports = router